package com.example.demo.actions;

import com.example.demo.services.DependencyAnalyzerService;
import com.example.demo.utils.VisualizationUtils;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.ui.Messages;

import java.nio.file.Path;
import java.util.Map;
import java.util.Set;

public class AnalyzeDependenciesAction extends AnAction {
<<<<<<< HEAD


    /**
     * 用户点击 Analyze Dependencies 操作。
     * 插件调用 DependencyAnalyzerService 进行依赖分析。
     * 生成的依赖数据传递给 VisualizationUtils.visualizeDependencies。
     * VisualizationUtils 生成图片，并使用 ImageDisplayDialog 直接展示结果。
     */
=======
>>>>>>> ae04cf21c481035528eaf247571d600f597507d9
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            Messages.showErrorDialog("No project found!", "Error");
            return;
        }

<<<<<<< HEAD
        // 获取项目的根路径
        String projectBasePath = project.getBasePath();
        if (projectBasePath == null) {
=======
        VirtualFile baseDir = project.getBaseDir();
        if (baseDir == null) {
>>>>>>> ae04cf21c481035528eaf247571d600f597507d9
            Messages.showErrorDialog("No project base directory found!", "Error");
            return;
        }

        DependencyAnalyzerService analyzerService = new DependencyAnalyzerService();
        try {
<<<<<<< HEAD
            // 将项目根路径转换为 Path 对象
            Path projectPath = Path.of(projectBasePath);
            Map<String, Set<String>> dependencies = analyzerService.analyzeProjectDependencies(projectPath);

            // 调用可视化工具展示结果
            VisualizationUtils.visualizeDependencies(dependencies, e.getProject());
=======
            Map<String, Set<String>> dependencies = analyzerService.analyzeProjectDependencies(Path.of(baseDir.getPath()));
            // 调用可视化工具展示结果
            VisualizationUtils.visualizeDependencies(dependencies);
>>>>>>> ae04cf21c481035528eaf247571d600f597507d9
        } catch (Exception ex) {
            Messages.showErrorDialog("Failed to analyze dependencies: " + ex.getMessage(), "Error");
        }
    }
}
